﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Tasks;
using DeepForest.Phone.Assets.Tools;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;

namespace unifiqAnthony
{
    public partial class AddListing : PhoneApplicationPage
    {
        int uid; // User ID
        PhotoChooserTask photoChooserTask;
        int photoSelected = 0;
        //List<PhotoResult> photoList;
        List<byte[]> byteList;

        public AddListing()
        {
            InitializeComponent();
            progressOverlay.Show();

            var obj = App.Current as App;
            uid = obj.uid;

            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

            byteList = new List<byte[]>();
            byteList.Add(null);
            byteList.Add(null);
            byteList.Add(null);
 
            // Get User
            UnifiqSvc.ServiceSoapClient dbSvc = new UnifiqSvc.ServiceSoapClient();
            dbSvc.getUUserCompleted += new EventHandler<UnifiqSvc.getUUserCompletedEventArgs>(dbSvc_UUserCompleted);
            dbSvc.getUUserAsync(uid);

        }

        void dbSvc_UUserCompleted(object sender, UnifiqSvc.getUUserCompletedEventArgs e)
        {
            UnifiqSvc.UUser u = e.Result;

            if (u != null)
            {
                progressOverlay.Hide();
                // Set Name to 'Posting As'
                tb_fullName.Text = u.name;
            }
            else
            {
                NotificationTool.Show(
                "Error",
                "User Not Found",
                new NotificationAction("Ok", () => { NavigationService.GoBack(); }));
            }
        }

        private void btn_next_Click(object sender, RoutedEventArgs e)
        {
            bool valid = true;
            StringBuilder builder = new StringBuilder();

            if (tb_stName.Text == "")
            {
                builder.Append("* Street Name is required \n");
                valid = false;
            }
            if (tb_stNo.Text == "")
            {
                //builder.Append("* Street No. is required \n");
                //valid = false;
            }
            if (tb_blkNo.Text == "")
            {
                builder.Append("* Blk No. is required \n");
                valid = false;
            }
            if (tb_doorNo.Text == "")
            {
                builder.Append("* Door No. is required \n");
                valid = false;
            }
            if (tb_apptNo.Text == "")
            {
                builder.Append("* Street No. is required \n");
                valid = false;
            }
            if (tb_zipCode.Text == "")
            {
                builder.Append("* Zip Code is required \n");
                valid = false;
            }
            //tb_bldngName.Text;
            if (tb_contactNo.Text == "")
            {
                builder.Append("* Contact No. is required \n");
                valid = false;
            }
            else if (tb_contactNo.Text.Length < 8)
            {
                builder.Append("* Enter a local 8 digit phone number \n");
                valid = false;
            }
            //tb_bldngName.Text;
            if (tb_fullName.Text == "")
            {
                builder.Append("* Name is required \n");
                valid = false;
            }
            //tb_desc.Text;
            if (tb_noOfTenants.Text == "")
            {
                builder.Append("* Max No. of Tenants is required \n");
                valid = false;
            }
            if (tb_fee.Text == "")
            {
                builder.Append("* Monthly Fee is required \n");
                valid = false;
            }

            //valid = true; // TEST DATA

            if (valid == false)
            {
                NotificationTool.Show(
                "Missing / Invalid Fields",
                builder.ToString(),
                new NotificationAction("OK", () => { }));
            }
            else
            {
                prepareData();
                NavigationService.Navigate(new Uri("/ConfirmListing.xaml", UriKind.Relative));
            }
        }

        void prepareData()
        {
            var obj = App.Current as App;

            UnifiqSvc.Listing l = new UnifiqSvc.Listing();
            l.uid = uid;
            l.stName = tb_stName.Text;
            l.stNo = tb_stNo.Text;
            l.blkNo = tb_blkNo.Text;
            l.doorNo = tb_doorNo.Text;
            l.apptNo = tb_apptNo.Text;
            l.zipCode = tb_zipCode.Text;
            l.bldngName = tb_bldngName.Text;
            l.contactNo = tb_contactNo.Text;
            l.description = tb_desc.Text;
            l.contactName = tb_fullName.Text;
            l.noOfTenants = Int32.Parse(tb_noOfTenants.Text);
            l.fee = Double.Parse(tb_fee.Text);

            // Set listing to App
            obj.listing = l;

            // Set photolist to App
            obj.byteList = byteList;
        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {

                //Code to display the photo on the page in an image control named myImage.
                BitmapImage bmp = new BitmapImage();

                if (photoSelected==1)
                {
                    byteList[0] = ChosenPhotoToBytes(e.ChosenPhoto);
                    bmp.SetSource(e.ChosenPhoto);
                    image1.Source = bmp;
                }
                else if(photoSelected==2)
                {
                    byteList[1] = ChosenPhotoToBytes(e.ChosenPhoto);
                    bmp.SetSource(e.ChosenPhoto);
                    image2.Source = bmp;
                }
                else
                {
                    byteList[2] = ChosenPhotoToBytes(e.ChosenPhoto);
                    bmp.SetSource(e.ChosenPhoto);
                    image3.Source = bmp;
                }
                
            }
        }

        private byte[] ChosenPhotoToBytes(Stream s)
        {
            //Convert the photo to bytes
            byte[] PhotoBytes = new byte[s.Length];

            // rewind first
            s.Position = 0;

            // now succeeds
            s.Read(PhotoBytes, 0, PhotoBytes.Length);

            return PhotoBytes;
        }

        private void btn_image1_Click(object sender, RoutedEventArgs e)
        {
            photoChooserTask.ShowCamera = true;
            photoChooserTask.Show();
            photoSelected = 1;
        }

        private void btn_image2_Click(object sender, RoutedEventArgs e)
        {
            photoChooserTask.ShowCamera = true;
            photoChooserTask.Show();
            photoSelected = 2;
        }

        private void btn_image3_Click(object sender, RoutedEventArgs e)
        {
            photoChooserTask.ShowCamera = true;
            photoChooserTask.Show();
            photoSelected = 3;
        }

    }
}